Page({
  data: {
    officerName: '',
    statusDescription: '',
    hasSigned: false // 在岗状态
  },
  
  onLoad(options) {
    this.loadOfficerInfo();
  },
  
  onShow() {
    // 每次页面显示时重新加载警员信息
    this.loadOfficerInfo();
  },
  
  loadOfficerInfo() {
    try {
      const currentOfficer = wx.getStorageSync('currentOfficer');
      if (currentOfficer) {
        this.setData({
          officerName: currentOfficer.name || '',
          statusDescription: currentOfficer.statusDescription || '',
          hasSigned: currentOfficer.statusDescription === '在岗'
        });
      }
    } catch (e) {
      console.error('获取警员信息失败', e);
    }
  },
  
  toggleSign() {
    // 更新本地状态
    const newStatus = !this.data.hasSigned;
    this.setData({
      hasSigned: newStatus,
      statusDescription: newStatus ? '在岗' : '离线'
    });
    
    // 更新缓存中的警员信息
    try {
      const currentOfficer = wx.getStorageSync('currentOfficer') || {};
      currentOfficer.statusDescription = newStatus ? '在岗' : '离线';
      wx.setStorageSync('currentOfficer', currentOfficer);
      
      // 显示提示
      wx.showToast({
        title: newStatus ? '已切换为在岗状态' : '已切换为离线状态',
        icon: 'none'
      });
    } catch (e) {
      console.error('更新警员状态失败', e);
      wx.showToast({
        title: '状态更新失败',
        icon: 'none'
      });
    }
  },
  
  logout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除本地存储
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('currentOfficer');
          
          // 跳转到登录页
          wx.navigateTo({
            url: '/pages/authorize/authorize'
          });
        }
      }
    });
  },
  
  navigateToEquipment() {
    wx.navigateTo({
      url: '/pages/equipment/equipment'
    });
  },
  
  navigateToAssignment() {
    wx.navigateTo({
      url: '/pages/assignment/assignment'
    });
  },
  
  navigateToHelp() {
    wx.navigateTo({
      url: '/pages/help/help'
    });
  },

  goToArchive() {
    wx.navigateTo({
      url: '/pages/my-archive/my-archive'
    });
  }
})